import { createApp } from 'vue'
import '@/styles/element/index.scss';
import App from './App.vue'
import router from './router'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import './styles/app.css' // 引入 app.css
import axios from 'axios'
import Vueaxios from 'vue-axios'

createApp(App).use(store).use(router).use(Vueaxios,axios).use(ElementPlus).mount('#app')

// axios全局设置后端地址
axios.defaults.baseURL="http://localhost:8085"
localStorage.setItem("baseURL","http://localhost:8085")

// 请求拦截器：在请求发出去之前执行
axios.interceptors.request.use((config)=>{
    config.headers.token=localStorage.getItem("token")
    return  config;
})

// 响应拦截器
axios.interceptors.response.use(resp=>{
    let  r=resp.data;
    if(r.code===600){
        alert(r.msg);
        router.push("/login")
        return;
    }
    return  resp;
})


router.beforeEach((to, from, next) => {
    // 更新面包屑
    store.commit('setBreadcrumbs', to.matched);

    if (to.meta && to.meta.title) {
        // 检查当前路由是否是 '/home/mine' 的子路由
        if (to.path.startsWith('/home/mine') && to.path !== '/home/mine') {
            // 如果是 '/home/mine' 的子路由，不添加标签
            next();
        } else {
            // 否则，正常添加标签
            store.commit('addTag', { name: to.meta.title, path: to.path });
            store.commit('setActiveTag', to.path);
            next();
        }
    } else {
        next();
    }
});
